package com.jic.market.impl.rest.allocation;

import com.jic.common.base.vo.RestResult;
import com.jic.market.common.constant.DbEffectConstant;
import com.jic.market.common.exception.MarketErrorCodeEnum;
import com.jic.market.common.exception.MarketException;
import com.jic.market.entity.AllocationModel;
import com.jic.market.request.rest.allocation.AllocationThemeInfoInner;
import com.jic.market.request.rest.allocation.CollectionThemeInfoRequest;
import com.jic.market.request.rest.allocation.QueryAllocationAndThemeInfoRequest;
import com.jic.market.request.rest.allocation.UpdateAllocationInfoRequest;
import com.jic.market.response.rest.allocation.QueryAllocationAndThemeInfoResponse;
import com.jic.market.service.AllocationService;
import com.jic.market.service.rest.allocation.AllocationRestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xuyongliang
 * @version V1.0
 * @className AllocationRestServiceImpl
 * @description 分摊H5服务
 * @date 2020/4/10
 */
@Slf4j
@Service
public class AllocationRestServiceImpl implements AllocationRestService {

    @Resource
    private AllocationService allocationService;

    @Resource
    private QueryPromotionForAllocationStrategyContext queryPromotionContext;

    @Override
    public RestResult<List<QueryAllocationAndThemeInfoResponse>> queryAllocationAndThemeInfo(QueryAllocationAndThemeInfoRequest request) {

        List<QueryAllocationAndThemeInfoResponse> responseList = new ArrayList<>();
        List<AllocationThemeInfoInner> allocationThemeInfoList = request.getAllocationThemeInfoList();
        for (AllocationThemeInfoInner allocationThemeInfoInner : allocationThemeInfoList) {
            QueryAllocationAndThemeInfoResponse infoResp =new QueryAllocationAndThemeInfoResponse();

            String promotionCode = allocationThemeInfoInner.getPromotionCode();
            // 查询分摊信息
            AllocationModel allocationCondition = new AllocationModel();
            allocationCondition.setPromotionId(promotionCode);
            AllocationModel allocationModel = allocationService.allocationQuery(allocationCondition);
            if(allocationModel == null){
                log.error("queryAllocationAndThemeInfo 无此分摊比例配置 promotionCode = {}", promotionCode );
                throw new MarketException(MarketErrorCodeEnum.ALLOCATION_NOT_EXISTE_ERROR);
            }
            infoResp.setCostAllocationFlag(allocationModel.getCostAllocationFlag());
            infoResp.setParentAllocationAmount(allocationModel.getParentAllocationAmount());
            infoResp.setChildAllocationScaleAmount(allocationModel.getChildAllocationScaleAmount());
            infoResp.setMerchantAllocationAmount(allocationModel.getMerchantAllocationAmount());
            infoResp.setStoreAllocationAmout(allocationModel.getStoreAllocationAmout());
            infoResp.setDeptType(allocationModel.getDeptType());
            infoResp.setPromName(allocationModel.getPromName());
            Integer promType = allocationModel.getPromType();

            CollectionThemeInfoRequest collectionThemeInfoReq =new CollectionThemeInfoRequest();
            collectionThemeInfoReq.setPromotionCode(promotionCode);
            collectionThemeInfoReq.setPromType(promType);
            // 设置活动信息查询上下文
            QueryAllocationAndThemeInfoResponse themeInfo = queryPromotionContext.collectionThemeInfo(collectionThemeInfoReq);
            if(themeInfo == null){
                log.error("queryAllocationAndThemeInfo 无此分摊比例活动配置 promotionCode = {}, promType = {}", promotionCode, promType);
                throw new MarketException(MarketErrorCodeEnum.ALLOCATION_THEME_NOT_EXISTE_ERROR);
            }
            infoResp.setMailType(themeInfo.getMailType());
            infoResp.setStartTime(themeInfo.getStartTime());
            infoResp.setEndTime(themeInfo.getEndTime());
            infoResp.setProUserType(themeInfo.getProUserType());
            infoResp.setCustomerLimit(themeInfo.getCustomerLimit());
            infoResp.setOfferedCustomerLimit(themeInfo.getOfferedCustomerLimit());
            responseList.add(infoResp);
        }

        return RestResult.success(responseList);

    }

    @Override
    public RestResult<Boolean> updateAllocationInfo(UpdateAllocationInfoRequest request) {
        if(request.getChildAllocationScaleAmount() == null
                && request.getParentAllocationAmount() == null
                && request.getMerchantAllocationAmount() == null
                && request.getStoreAllocationAmout() == null){
            return RestResult.success(Boolean.TRUE);
        }
        AllocationModel modifyEntity =new AllocationModel();
        modifyEntity.setParentAllocationAmount(request.getParentAllocationAmount());
        modifyEntity.setChildAllocationScaleAmount(request.getChildAllocationScaleAmount());
        modifyEntity.setMerchantAllocationAmount(request.getMerchantAllocationAmount());
        modifyEntity.setStoreAllocationAmout(request.getStoreAllocationAmout());
        AllocationModel whereCondition = new AllocationModel();
        whereCondition.setPromotionId(request.getPromotionCode());
        Integer count = allocationService.allocationEdit(modifyEntity, whereCondition);

        if(DbEffectConstant.UPDATE_SUCCESS.equals(count)){
            return RestResult.success(Boolean.TRUE);
        }
        return RestResult.success(Boolean.FALSE);
    }
}
